﻿var mainGrid = null;

function InitLayout() {
    $('body').layout({
        defaults: {}
		, north: {
		    size: "auto"
			, spacing_open: 0
			, closable: false
			, resizable: false
		}
		, west: {
		    size: 180
			, spacing_closed: 22
			, togglerLength_closed: 140
			, togglerAlign_closed: "top"
			, togglerContent_closed: "M<BR>e<BR>n<BR>u"
			, slideTrigger_open: "mouseover"
		}
    });

    $('.ui-layout-north')
        .css("z-index", 200)
        .css("overflow", "visible");

    //    $('ul.top-menu > li').bind('mouseover', jsddm_open)
    //    $('ul.top-menu > li').bind('mouseout', jsddm_timer)

//    $('.js-enabled').memu({
//        width: 200
//    });
    
}

function NonModalOpen(nonModalUrl, grid) {
    mainGrid = grid;

    window.open(nonModalUrl, "_blank");
}

function ModalOpen(modalUrl, grid) {
    var varFromModal;
    var args = { 'callback': function (value) {
        varFromModal = value;

        if(grid)
            grid.trigger("reloadGrid");
    } };

    mainGrid = grid;

    var scr_w = Math.round(screen.width * 0.8);
    var scr_h = Math.round(screen.height * 0.8);
    var scr_t = Math.round((screen.width - scr_w) / 2);
    var scr_l = Math.round((screen.height - scr_h) / 2);

    var feats = 'dialogHeight=' + scr_h + 'px; dialogWidth=' + scr_w + 'px; dialogTop=' + scr_t + 'px; dialogLeft=' + scr_l + 'px;  resizable=yes';
    window.showModalDialog(modalUrl, args, feats);
}

function ModalReturn(valueToReturn) {
    try{ window.dialogArguments.callback(valueToReturn); }
    catch (ex) { }

    if (window.opener && window.opener.mainGrid) 
        window.opener.mainGrid.trigger("reloadGrid");

    window.close();
}



/* http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript */
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}



//// http://javascript-array.com/scripts/jquery_simple_drop_down_menu/

//var timeout = 500;
//var closetimer = 0;
//var ddmenuitem = 0;

//function jsddm_open() {
//    jsddm_canceltimer();
//    jsddm_close();
//    //ddmenuitem = $(this).find('ul').css('visibility', 'visible');
//    ddmenuitem = $(this).find('ul').css('display', 'block');
//}

//function jsddm_close() {
//    if (ddmenuitem)
//        //ddmenuitem.css('visibility', 'hidden'); 
//        ddmenuitem.css('display', 'none'); 
//}

//function jsddm_timer()
//{ closetimer = window.setTimeout(jsddm_close, timeout); }

//function jsddm_canceltimer() {
//    if (closetimer) {
//        window.clearTimeout(closetimer);
//        closetimer = null;
//    }
//}

//document.onclick = jsddm_close;




function ParseFilter(filterValue) {
    if (filterValue == null || filterValue == undefined)
        return null;

    var aux = null;
    if (typeof filterValue == "string")
        aux = jQuery.parseJSON(filterValue);
    else
        aux = filterValue;

    if (!aux || !aux.groupOp) {
        return null;
    }
    else {
        // clean filter values
        var retFilter = { groupOp: aux.groupOp, rules: [], groups: [] };

        if (aux.rules) {
            for (var idx = 0; idx < aux.rules.length; idx++) {
                var rule = ParseFilterRule(aux.rules[idx]);

                if (rule)
                    retFilter.rules.push(rule);
            }
        }

        if (aux.groups) {
            for (var idx = 0; idx < aux.groups.length; idx++) {
                var group = ParseFilter(aux.groups[idx]);

                if (group)
                    retFilter.groups.push(group);
            }
        }

        if (retFilter.rules.length <= 0 && retFilter.groups.length <= 0)
            return null;

        return retFilter;
    }
}
function ParseFilterRule(ruleValue) {
    if (ruleValue == null || ruleValue == undefined)
        return null;

    try {
        var rule = { field: ruleValue.field, op: ruleValue.op, data: ruleValue.data };

        return rule;
    } catch(ex) {
        return null;
    }
}


// Joins filters and returns the resulted string
function JoinFilters(leftFilter, rightFilter) {
    var lJSONFilter = ParseFilter(leftFilter);
    var rJSONFilter = ParseFilter(rightFilter);
    
    if (!lJSONFilter) {
        if (!rJSONFilter) // check if also the right JSON filter is null
            return "";
        else
            return $.toJSON(rJSONFilter);
    } else {
        if (!rJSONFilter) {  // check if the right JSON filter is null
            return $.toJSON(lJSONFilter);
        }
        else {
            if (lJSONFilter.groupOp.toUpperCase() == "AND") {
                if (rJSONFilter.groupOp.toUpperCase() == "AND") {
                    // copy right into left
                    if (lJSONFilter.rules == null || lJSONFilter.rules == undefined) lJSONFilter.rules = [];
                    if (rJSONFilter.rules) {
                        for (var idx = 0; idx < rJSONFilter.rules.length; idx++)
                            lJSONFilter.rules.push(rJSONFilter.rules[idx]);
                    }

                    if (lJSONFilter.groups == null || lJSONFilter.groups == undefined) lJSONFilter.groups = [];
                    if (rJSONFilter.groups) {
                        for (var idx = 0; idx < rJSONFilter.groups.length; idx++)
                            lJSONFilter.groups.push(rJSONFilter.groups[idx]);
                    }

                    return $.toJSON(lJSONFilter);
                } else {
                    if (lJSONFilter.groups == null || lJSONFilter.groups == undefined)
                        lJSONFilter.groups = [];

                    lJSONFilter.groups.push(rJSONFilter);

                    return $.toJSON(lJSONFilter);
                }
            } else { // lfilter is OR
                if (rJSONFilter.groupOp.toUpperCase() == "AND") {
                    if (rJSONFilter.groups == null || rJSONFilter.groups == undefined)
                        rJSONFilter.groups = [];

                    rJSONFilter.groups.push(lJSONFilter);

                    return $.toJSON(rJSONFilter);
                } else {
                    // copy right into left
                    if (lJSONFilter.rules == null || lJSONFilter.rules == undefined) lJSONFilter.rules = [];
                    if (rJSONFilter.rules) {
                        for (var idx = 0; idx < rJSONFilter.rules.length; idx++)
                            lJSONFilter.rules.push(rJSONFilter.rules[idx]);
                    }

                    if (lJSONFilter.groups == null || lJSONFilter.groups == undefined) lJSONFilter.groups = [];
                    if (rJSONFilter.groups) {
                        for (var idx = 0; idx < rJSONFilter.groups.length; idx++)
                            lJSONFilter.groups.push(rJSONFilter.groups[idx]);
                    }

                    return $.toJSON(lJSONFilter);
                }
            }
        }
    }
}